import { request } from '@/service/request'
import Taro from '@tarojs/taro';

export async function getDemographicInfo(num: string) {
  const res = await request.get<any>(
    "/health_records/HealthRecord/healthRecordInfo/list",
    {
      // idNumber:idcard,
      tel: num
    },
    {
      useErrMsg: true,
    }
  );
  console.log(res, "res===============");

  if (res?.success?.records) {
    return res.success.records[0];
  }
  return null;
}

export async function getFamilyByIdcard(idcard: string) {
  // const params: any = Taro.getCurrentInstance().router?.params;
  const result = await request.get<any>(
    "/nurse_user/nurseUser/list",
    {
      idcard: idcard,
    },
    { useErrMsg: true }
  );
  console.log(result, "result---------===========");

  let ary: any[] = [];
  result.success.records.forEach(async (item: any) => {
    const rs = await request.get<any>(
      "/nurse_user/nurseUser/listNurseUserFamilyByMainId",
      {
        userId: item.id,
      },
      { useErrMsg: true }
    );
    ary = [...ary, ...rs.success.records];
    console.log(rs.success.records, "rs- all --------=======================");
  });
  console.log(ary, "ary---------===========");
  return ary;
}

export async function getNurseUserByOpenId(openid: string, phone: string) {
  const result = await request.get<any>(
    "/nurse_user/nurseUser/queryByOpenId",
    {
      openid: openid,
    },
  );
  // console.log(result, "result---------===========");
  let obj = null;
  result.success.nurseUserFamilyList.forEach((item: any) => {
    if(item.phone === phone){
      obj = item;
    }
  });
  return obj;
}
